package com.example.gkcircle.utils;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

import java.util.ArrayList;

/**
 * Created by Jehu on 16/3/18.
 */
public class ActivityUtil {



    private final String TAG="ActivityUtil";

    private ArrayList<Activity> activityList = new ArrayList<Activity>();

    private static ActivityUtil activityUtil;

    private ActivityUtil(){

    }

    public static ActivityUtil getInstance(){
        if(null == activityUtil){
            activityUtil = new ActivityUtil();
        }
        return activityUtil;
    }

    //获取栈顶的activity
    public Activity getTopActivity(){
        return activityList.get(activityList.size()-1);
    }

    //activity入栈
    public void addActivity(Activity ac){
        activityList.add(ac);
        for(int i=0;i<activityList.size();i++){
            Log.i(TAG, "addActivity: "+activityList.get(i));
        }
    }

    //移除一个activity
    public void popOneActivity(Activity activity) {
        if (activityList != null && activityList.size() > 0) {
            if (activity != null) {
                activity.finish();
                activityList.remove(activity);
                Log.i(TAG, "popOneActivity: "+activity+"已出栈");
                activity = null;
            }
        }
    }
    //移除所有activity
    public void removeAllActivity(){
        for(Activity ac:activityList){
            if(null != ac){
                if(!ac.isFinishing()){
                    ac.finish();
                }
                ac = null;
            }
        }
        activityList.clear();
    }






    //启动Activity
    public static void startActivity(Activity activity,Class c){

        activity.startActivity(new Intent(activity, c));

    }
}
